﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StackQueueHash
{
    class RevertStack
    {
        public void ReverseStack(Stack<int> stk)
        {
            if (stk.Count < 1)
                return;

            int temp = stk.Pop();
            ReverseStack(stk);
            RecursivePush(temp, stk);

        }

        private void RecursivePush(int val, Stack<int> stk)
        {
            if (stk.Count < 1)
            {
                stk.Push(val);
                return;
            }

            int temp = stk.Pop();
            RecursivePush(val, stk);
            stk.Push(temp);
        }

        public void PrintStack(Stack<int> stk)
        {
            Stack<int> s2 = new Stack<int>();
            
            while (stk.Count > 0)
            {
                int t = stk.Pop();
                Console.Write("{0} ", t);
                s2.Push(t);
            }
            Console.WriteLine();

            while (s2.Count > 0)
            {
                stk.Push(s2.Pop());
            }
        }

        static void Main(string[] args)
        {
            Stack<int> stk = new Stack<int>();
            stk.Push(1);
            stk.Push(2);
            stk.Push(3);

            RevertStack rstk = new RevertStack();
            rstk.PrintStack(stk);

            Console.WriteLine("print second time...");
            rstk.PrintStack(stk);

            rstk.ReverseStack(stk);
            Console.WriteLine("after revert...");
            rstk.PrintStack(stk);

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();


        }
    }
}
